package cn.ibobo.lease.admin.service.impl;

import cn.ibobo.lease.common.base.BaseMapper;
import cn.ibobo.lease.common.base.BaseServiceImpl;
import cn.ibobo.lease.admin.dao.LeaseResDetailMapper;
import cn.ibobo.lease.admin.service.LeaseResDetailService;
import cn.ibobo.lease.common.dto.LeaseResDetailDTO;
import cn.ibobo.lease.common.enums.ResultEnum;
import cn.ibobo.lease.common.util.UuidUtil;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.stereotype.Service;
import java.util.List;


/**
 * 租赁物品详情表业务处理接口
 * @Author lililiang
 * @Date 2018-12-23 10:10:30
 */
@Service
public class LeaseResDetailServiceImpl extends BaseServiceImpl<LeaseResDetailDTO, String> implements LeaseResDetailService {

    private static final Log LOGGER = LogFactory.getLog(LeaseResDetailServiceImpl.class);

    @Autowired
    private LeaseResDetailMapper mapper;

    @Override
    protected BaseMapper<LeaseResDetailDTO, String> getMapper() {
        return mapper;
    }

    /**
     * 租赁物品详情表列表
     * @Author lililiang
     * @Date 2018-12-23 10:10:30
     * @Param
     * @Return
     */
    @Override
    public List<LeaseResDetailDTO> list4Page(LeaseResDetailDTO entity, Integer pageNum, Integer... pageSize) {
        return super.list4Page(entity, pageNum, pageSize);
    }

    /**
     * 租赁物品详情表列表（无分页）
     * @Author lililiang
     * @Date 2018-12-23 10:10:30
     * @Param
     * @Return
     */
    @Override
    public List<LeaseResDetailDTO> listByCondition(LeaseResDetailDTO entity) {
        return super.listByCondition(entity);
    }

    /**
     * 查询租赁物品详情表详情信息
     * @Author lililiang
     * @Date 2018-12-23 10:10:30
     * @Param
     * @Return
     */
    @Override
    public LeaseResDetailDTO queryOneByPk(String id) {
        return super.queryOneByPk(id);
    }

    /**
     * 保存租赁物品详情表信息
     * @Author lililiang
     * @Date 2018-12-23 10:10:30
     * @Param
     * @Return
     */
    @Override
    @Transactional
    public Integer save(LeaseResDetailDTO entity) {
        String did = entity.getDid();
        if(this.checkRepeat(entity)){
            return ResultEnum.CODE_REPEAT.getCode();
        }
        if(did == null){
            entity.setDid(UuidUtil.getUuid());
            if(entity.getCode() == null){
                entity.setCode(UuidUtil.getUuid());
            }
            return mapper.add(entity);
        }else {
            return mapper.edit(entity);
        }
    }

    /**
     * 校验编号重复
     * @Author lililiang
     * @Date 2018-12-23 10:10:30
     * @Param
     * @Return
     */
    private Boolean checkRepeat(LeaseResDetailDTO entity) {
        LeaseResDetailDTO tempDTO = new LeaseResDetailDTO();
        tempDTO.setCode(entity.getCode());
        List<LeaseResDetailDTO> leaseResDetailDTOS = mapper.listByCondition(tempDTO);
        if(leaseResDetailDTOS == null || leaseResDetailDTOS.size() == 0){
            return false;
        }else if(leaseResDetailDTOS.size() == 1 && leaseResDetailDTOS.get(0).getDid().equals(entity.getDid())){
            return false;
        }else{
            return true;
        }
    }

    /**
     * 删除租赁物品详情表信息
     * @Author lililiang
     * @Date 2018-12-23 10:10:30
     * @Param
     * @Return
     */
    @Override
    @Transactional
    public Integer delete(String id) {
        return super.delete(id);
    }
}
